layui.use(['layer', 'element', 'laytpl', 'jquery', 'table'], function() {
	//引入模块
	var layer = layui.layer,
		element = layui.element,
		form = layui.form,
		laytpl = layui.laytpl,
		table = layui.table,
		laydate = layui.laydate,
		$ = layui.jquery;
	form.render();

	if (localStorage.set != null) {
		//加载页面发生请求拿到数据
		$.ajax({
			url: "http://localhost:8080/sc/admin?type=home",
			async: true, //异步
			type: "POST",
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				//这里找id必须用js
				if (data.code == 'TokenFailed') {
					//删除token set
					delete localStorage.token;
					delete localStorage.set;
					layer.msg('登录失效，请重新登录！')
					setTimeout(function() {
						//调用父页面方法
						parent.tz();
					}, 1000);
				} else {
					var getTpl = document.getElementById('homemb').innerHTML,
						view = document.getElementById('homemoban');
					laytpl(getTpl).render(data, function(html) {
						view.innerHTML = html;
					});
				}
			}
		});

	} else {
		layer.msg('您还没有登录！')
		setTimeout(function() {
			parent.tz();
			//window.location.replace('admin_login.html');
		}, 1000);
	}

	//添加账号的失去焦点事件
	$('#addusername').blur(function() {
		var username = $('#addusername').val()
		$.ajax({
			url: "http://localhost:8080/sc/admin3?type=verification&username=" + username,
			async: true, //异步
			type: "POST",
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				if (data.code == 'Success') {
					layer.tips('账号可以使用哦！', '#addusername', {
						tips: [1, '#00aaff'],
						time: 4000
					});
					return;
				}
				layer.tips('账号不可用哦！', '#addusername', {
					tips: [1, '#ff0000'],
					time: 4000
				});
			}
		});
	})

	//日期
	laydate.render({
		elem: '#date'
	});
	//自动重载
	var token = localStorage.token;
	//表格重载
	table.reload('table_id', {
		url: 'http://localhost:8080/sc/admin3?type=adminm',
		headers: {
			token: token
		} //设定异步数据接口的额外参数
	});

	//头工具栏事件
	table.on('toolbar(table_filter)', function(obj) {
		switch (obj.event) {
			case 'addzh': //添加账号
				//判断权限
				//拉取信息
				$.ajax({
					url: "http://localhost:8080/sc/admin3?type=Adminqx",
					async: true, //异步
					type: "POST",
					headers: {
						"token": localStorage.token
					},
					success: function(data) {
						if (data == '99') {
							//弹出窗口
							layer.open({
								type: 1,
								area: ['600px', '530px'],
								title: '添加管理员',
								content: $("#admin_add_modify"),
								shade: 0,
								btn: ['添加'],
								btn1: function(index, layero) {
									$('#admin_add_tj').click();
								},
								cancel: function(layero, index) {
									layer.closeAll();
									$('#admin_re').click();
								}
							});
						} else {
							layer.alert('您没有权限哦！', {
								icon: 1,
								skin: 'layer-ext-demo' //见：扩展说明
							})
						}
					}
				});
				break;
		};
	});

	//行工具栏事件
	table.on('tool(table_filter)', function(obj) {
		var shuju = obj.data; //拿到数据
		switch (obj.event) {
			case 'update': //修改
				//拉取信息
				$.ajax({
					url: "http://localhost:8080/sc/admin3?type=getAdminxx&id=" + shuju.id,
					async: true, //异步
					type: "POST",
					headers: {
						"token": localStorage.token
					},
					success: function(data) {
						var mail = data.mail;
						var username = data.username;
						var password = data.password;
						var sex = data.sex;
						var name = data.name;
						//设置属性
						$("#username").val(username); //账号
						$("#name").val(name); //名字
						$("#password").val(password); //密码
						if (sex == '女') {
							$("#nv").prop("checked", true); //性别
						} else {
							$("#nan").prop("checked", true); //性别
						}
						$("#email").val(mail); //邮箱
						form.render();

					}
				});
				//弹出窗口
				layer.open({
					type: 1,
					area: ['600px', '430px'],
					title: '修改管理员信息',
					content: $("#admin_update_modify"),
					shade: 0,
					btn: ['修改'],
					btn1: function(index, layero) {
						$('#admin_update_tj').click();
					},
					cancel: function(layero, index) {
						layer.closeAll();
					}
				});
				break;
			case 'del': //删除
				layer.prompt({
					title: '输入正确口令才能执行下一步！',
					formType: 1
				}, function(pass, index) {
					if (pass != '884500') {
						layer.close(index);
						layer.msg('口令错误')
						return;
					}
					layer.close(index);
					layer.prompt({
						title: '输入最后一遍执行删除请想清楚！',
						formType: 1
					}, function(pass1, index) {
						if (pass1 == '884500') {
							layer.close(index);
							$.ajax({
								url: "http://localhost:8080/sc/admin3?type=delAdminxx&id=" +
									shuju.id,
								async: true, //异步
								type: "POST",
								headers: {
									"token": localStorage.token
								},
								success: function(data) {
									//自动重载
									var token = localStorage.token;
									//表格重载
									table.reload('table_id', {
										url: 'http://localhost:8080/sc/admin3?type=adminm',
										headers: {
											token: token
										} //设定异步数据接口的额外参数
									});
									layer.alert(data.msg, {
										icon: 1,
										skin: 'layer-ext-demo' //见：扩展说明
									})
								}
							});
						} else {
							layer.close(index);
							layer.msg('口令不正确！要不算了吧！')
						}

					});
				});

				break;
			case 'look': // //查看
				layer.tips('请在这里查看哦！', '.admin_xxban', {
					tips: [1, '#3595CC'],
					time: 4000
				});
				//先清空徽章状态
				$('#admin_hz_gj').css('display', 'none');
				$('#admin_hz_pt').css('display', 'none');
				$.ajax({
					url: "http://localhost:8080/sc/admin3?type=lookAdmin&id=" + shuju.id,
					async: true, //异步
					type: "POST",
					headers: {
						"token": localStorage.token
					},
					success: function(data) {
						// 判断显示徽章
						if (data.ad.authority == '99') {
							$('#admin_hz_gj').css('display', 'inline-block');
						} else {
							$('#admin_hz_pt').css('display', 'inline-block');
						}
						//判断当前权限
						if (data.authority == '99') {
							$('#admin_xx_password').text(data.ad.password);
						} else {
							$('#admin_xx_password').text('无权限查看');
						}
						// 赋值
						$('#admin_xx_sex').text(data.ad.sex);
						$('#admin_xx_mail').text(data.ad.mail);
						$('#admin_xx_cishu').text(data.ad.portrait);
						$('#admin_xx_sr').text(data.ad.birthdate);
						$('#admin_xx_username').text(data.ad.username);
						$('#admin_xx_name').text(data.ad.name);
					}
				});
				break;
		};
	});

	//表单监听
	form.on('submit(admin_update_tj)', function(data) {
		//layer.msg(JSON.stringify(data.field));
		var loading = layer.load(1, {
			shade: [0.5, '#bcbcbc'] //0.1透明度的白色背景
		});
		$.ajax({
			url: "http://localhost:8080/sc/admin3?type=setAdminxx",
			async: true, //异步
			type: "POST",
			data: data.field, //拿到表单数据
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				//关闭加载窗口
				layer.close(loading);
				layer.alert(data.msg, {
					icon: 1,
					skin: 'layer-ext-demo' //见：扩展说明
				})
				setTimeout(function() { //延迟关闭
					layer.closeAll();
					location.reload(true);
				}, 1000);
			}
		})
		return false; //一定要有这个玩意儿不然不会执行ajax
	});

	//表单监听
	form.on('submit(admin_add_tj)', function(data) {
		//layer.msg(JSON.stringify(data.field));
		var loading = layer.load(1, {
			shade: [0.5, '#bcbcbc'] //0.1透明度的白色背景
		});
		$.ajax({
			url: "http://localhost:8080/sc/admin3?type=addAdminxx",
			async: true, //异步
			type: "POST",
			data: data.field, //拿到表单数据
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				//关闭加载窗口
				layer.close(loading);
				layer.closeAll();
				setTimeout(function() { //延迟关闭
					layer.alert(data.msg, {
						icon: 1,
						skin: 'layer-ext-demo' //见：扩展说明
					})
					//自动重载
					var token = localStorage.token;
					//表格重载
					table.reload('table_id', {
						url: 'http://localhost:8080/sc/admin3?type=adminm',
						headers: {
							token: token
						} //设定异步数据接口的额外参数
					});
				}, 1000);
				$('#admin_re').click();
			}
		})
		return false; //一定要有这个玩意儿不然不会执行ajax
	});

});
